/*
 * @Author: your name
 * @Date: 2021-03-25 16:47:21
 * @LastEditTime: 2021-04-07 19:58:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shop\training-group-5-warehouse\dianshang\vue.config.js
 */
const path = require("path");
const { userData } = require("./src//mock/login");

// 分类
const { classify } = require("./src/mock/classify");

//砍价
const { bargain, recommend } = require("./src/mock/bargain");

//直播特卖
const { live, barrage, shopList } = require("./src/mock/live");

//拼团
const { group, recommends } = require("./src/mock/group");

module.exports = {
  lintOnSave: false,
  devServer: {
    port: 4000,
    proxy: {
      "/apivue": {
        target: "http://1807a.gtxiaoyu.cn",
        pathRewrite: { "^/apivue": "" },
      },
      "/api": {
        target: "http://139.196.229.252:7001",
        pathRewrite: { "^/api": "/api" },
      },
    },
    before(app) {
      //登录
      app.get("/loginn", (req, res) => {
        const { username, password } = req.query;
        const bool = userData.some(
          (item) =>
            item.user + "" === username + "" && item.pass + "" === password + ""
        );
        const data = userData.filter(
          (item) =>
            item.user + "" === username + "" && item.pass + "" === password + ""
        );
        if (bool) {
          res.send({
            code: 1,
            data: {
              token: "666",
              username,
              data,
            },
          });
        } else {
          res.send({
            code: -1,
            msg: "请检查账号密码",
          });
        }
      });
      // 分类
      app.get("/api/classify", (req, res) => {
        res.send({
          code: 200,
          data: {
            classify,
          },
        });
      });
      //砍价免费拿
      app.get("/api/bargain", (req, res) => {
        res.send({
          code: 200,
          data: {
            bargain,
          },
        });
      });
      //砍价推荐相似
      app.get("/api/recommend", (req, res) => {
        res.send({
          code: 200,
          data: {
            recommend,
          },
        });
      });
      //直播特卖
      app.get("/api/live", (req, res) => {
        const { page, limit } = req.query;
        if ((page, limit)) {
          const pageList = live.filter(
            (item, index) => index < limit * page && index >= limit * (page - 1)
          );
          res.send({
            code: 200,
            data: {
              pageList,
            },
          });
        } else {
          const pageList = live;
          res.send({
            code: 200,
            data: {
              pageList,
            },
          });
        }
      });
      //直播间弹幕
      app.get("/api/barrage", (req, res) => {
        res.send({
          code: 200,
          data: {
            barrage,
          },
        });
      });
      //直播间商品列表
      app.get("/api/shoplist", (req, res) => {
        res.send({
          code: 200,
          data: {
            shopList,
          },
        });
      });
      //拼团
      app.get("/api/group", (req, res) => {
        res.send({
          code: 200,
          data: {
            group,
          },
        });
      });
      //拼团推荐相似
      app.get("/api/recommends", (req, res) => {
        res.send({
          code: 200,
          data: {
            recommends,
          },
        });
      });
    },
  },

  // webpack 配置进行更细粒度的修改  https://cli.vuejs.org/zh/config/#chainwebpack
  chainWebpack: (config) => {
    //修改文件引入自定义路径
    config.resolve.alias
      .set("@", path.resolve(__dirname, "./src"))
      .set("@c", path.resolve(__dirname, "./src/components"))
      .set("@css", path.resolve(__dirname, "./src/assets/css"))
      .set("@img", path.resolve(__dirname, "./src/assets/img"))
      .set("@views", path.resolve(__dirname, "./src/views"))

      .set("@js", path.resolve(__dirname, "./src/assets/js"));
    // 1. 修改svg规则，排除icons目录下的svg文件
    config.module
      .rule("svg")
      // 排除icon目录
      // exclude 排除 add 向数组里插入值
      .exclude.add(path.resolve(__dirname + "/src/icons"));
    // 2. 新增规则，仅打包icons目录下的svg
    config.module
      .rule("icons") // 规则名称 有则取 无则添加
      .test(/\.svg$/) // 匹配规则
      // 使用了add之后 会离开当前链的上下文 所以需要end再返回上一步
      .include.add(path.resolve(__dirname + "/src/icons")) // 仅匹配icons 下的 svg
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      });
  },
};
